<template>
  <!-- 实验室检查 -->
  <section>
    <div style="text-align:right;">
      <el-button @click="dialogLaboratoryTemplate=true">选择模板</el-button>
    </div>
    <br />
    <table width="100%" border="0">
      <tr v-for="(item,index) in form" :key="index">
        <td>
          <dir class="item-box" @click="onOpenRelation(item)">
            <div class="form-tool">
              <div>
                <el-link :underline="false" type="primary">{{item.checkName}}&nbsp;￥{{item.price}}</el-link>
              </div>
              <div>
                <el-badge value="本院" type="info" class="vertical-middle" style="height:18px;"></el-badge>&nbsp;&nbsp;
                <el-link v-if="!toDisabled(item)" :underline="false" class="vertical-middle" @click.stop="onDelItem(item,index)">
                  <i class="el-icon-close"></i>
                </el-link>
              </div>
            </div>
          </dir>
        </td>
        <td width="100" style="text-align:center;line-height:36px;vertical-align: top;">
          <!-- <el-input disabled :value="item.checkupNum+item.unit"></el-input> -->
          {{item.checkupNum}}次
        </td>
        <td width="300" style="vertical-align: top;">
          <el-input v-model="item.remark" placeholder="备注" :disabled="item.payStatus!=2"></el-input>
        </td>
      </tr>
    </table>
    <br />
    <div>
      <el-input ref="inputLab" placeholder="请选择实验室检查" @focus="onOpenLab" style="width:100%"></el-input>
    </div>
    <br />
    <div class="form-tool">
      <div>
        <el-button type="primary" :loading="btnLoading" @click="onSubmit">保存</el-button>
      </div>
      <div>
        <el-button v-if="form.length>0&&!templateSave.t3" @click="dialogPrint=true">
          <i class="iconfont icondayin vertical-middle" style="font-size:20px;"></i>
          <span class="vertical-middle">打印申请单</span>
        </el-button>
      </div>
    </div>
    <dialogLab v-model="dialogLab" @submit="onChangeCheckItem"></dialogLab>
    <dialogLaboratoryTemplate v-model="dialogLaboratoryTemplate" @change="onChangeTemplate"></dialogLaboratoryTemplate>
    <dialogRelationProject v-model="dialogRelationProject" :curItem="curItem"></dialogRelationProject>
    <print v-model="dialogPrint" :list="form" :patientId="patientId" :cureId="cureId"></print>
  </section>
</template>
<script>
export default {
  components: {
    dialogLaboratoryTemplate: () =>
      import("./_com/dialogLaboratoryTemplate.vue"),
    dialogRelationProject: () => import("./_com/dialogRelationProject.vue"),
    print: () => import("../_print/laboratory.vue"),
    dialogLab: () => import("../_com/dialogLab.vue")
  },
  computed: {
    templateSave() {
      return this.$store.state.doctorMZ.templateSave;
    },
    templateData() {
      return this.$store.state.doctorMZ.templateData;
    },
    //单位
    unitEnum() {
      return this.$store.state.doctorMZ.enum.unitEnum;
    },
    patientId() {
      return this.$store.state.doctorMZ.patientId;
    },
    doctorId() {
      return this.$store.state.doctorMZ.doctorId;
    },
    deptId() {
      return this.$store.state.doctorMZ.deptId;
    },
    cureId() {
      return this.$store.state.doctorMZ.cureId;
    }
  },
  data() {
    return {
      dialogLab: false,
      isOpenEdit: false,
      dialogPrint: false,
      form: [],
      curItem: null,
      cureMedicalRelateId: "",
      btnLoading: false,
      dialogRelationProject: false,
      dialogLaboratoryTemplate: false,
      laboratoryCheckArr: []
    };
  },
  created() {
  },
  mounted() {
    this.getData();
  },
  methods: {
    onSubmit() {
      let th = this;
      if (!th.cureMedicalRelateId && th.form.length == 0) {
        return;
      }
      let update = function() {
        let j = {
          cureMedicalRelateId: th.cureMedicalRelateId,
          cureCheckup: JSON.stringify(th.form)
        };
        th.btnLoading = true;
        th.$api
          .cureCheckupupdateV2(j)
          .then(res => {
            if (res.executed) {
              th.$message.success("保存成功");
              th.getData("reset");
            } else {
              th.$message.error(res.message);
            }
            th.btnLoading = false;
          })
          .catch(() => {
            th.btnLoading = false;
          });
      };
      let add = function() {
        let j = {
          cureMedicalRelateId: th.cureMedicalRelateId,
          cureCheckup: JSON.stringify(th.form)
        };
        th.btnLoading = true;
        th.$api
          .cureCheckupaddMultipleV2(j)
          .then(res => {
            if (res.executed) {
              th.$message.success("保存成功");
              th.getData("reset");
            } else {
              th.$message.error(res.message);
            }
            th.btnLoading = false;
          })
          .catch(() => {
            th.btnLoading = false;
          });
      };
      th.isOpenEdit = false;
      if (th.cureMedicalRelateId) {
        update();
      } else {
        add();
      }
    },

    //查看关联
    onOpenRelation(obj) {
      let th = this;
      th.curItem = obj;
      th.dialogRelationProject = true;
    },
    //删除项
    onDelItem(obj, index) {
      let th = this;
      if (obj.id) {
        let j = {
          cureCheckupId: obj.id
        };
        th.$api.cureCheckuppreDelete(j).then(res => {
          if (res.executed && res.data) {
            th.form.splice(index, 1);
          } else {
            th.$message.warning(res.message);
          }
        });
      } else {
        th.form.splice(index, 1);
      }
    },
    //选择模板
    onChangeTemplate(obj) {
      let th = this;
      // let arr = [];
      if (!obj.baseTemplateAdviceBeans) return;
      obj.baseTemplateAdviceBeans.forEach(item => {
        let j = {
          cureId: th.cureId,
          hpId: th.adminUser.hpId,
          patientId: th.patientId,
          deptId: th.deptId,
          doctorId: th.doctorId,
          checkupId: item.adviceId,
          checkupNum: item.count ? item.count : 1,
          checkName: item.adviceDesc,
          unit: item.unit ? item.unit : 0,
          price: item.retailPrice,
          sellMoney: item.retail_price,
          id: "",
          remark: item.remark,
          resultAddress: ""
        };
        // arr.push(j);
        th.addItem(j);
      });
      // th.form.push(...arr);
    },
    //选择检查项
    onChangeCheckItem(arr) {
      let th = this;
      arr.forEach(obj => {
        let j = {
          cureId: th.cureId,
          hpId: th.adminUser.hpId,
          patientId: th.patientId,
          deptId: th.deptId,
          doctorId: th.doctorId,
          checkupId: obj.id,
          checkupNum: 1,
          checkName: obj.name,
          // unit: obj.unit ? obj.unit : 0,
          price: obj.retailPrice,
          sellMoney: obj.retailPrice,
          id: "",
          remark: obj.remark,
          resultAddress: ""
        };
        th.addItem(j);
      });
    },
    //获取数据
    getData(reset) {
      let th = this;
      let j = {
        cureId: th.cureId
      };
      th.$api.cureCheckuplistcureId(j).then(res => {
        if (res.executed) {
          if (reset) {
            th.form = [];
          }
          res.data.forEach((d, index) => {
            if (index == 0) th.cureMedicalRelateId = d.cureMedicalRelateId;
            let j = {
              cureId: th.cureId,
              hpId: th.adminUser.hpId,
              patientId: th.patientId,
              deptId: th.deptId,
              doctorId: th.doctorId,
              checkupId: d.cureCheckupInfo.checkupId,
              checkupNum: d.cureCheckupInfo.checkupNum || 1,
              checkName: d.name,
              unit: d.unit ? d.unit : 0,
              sellMoney: 0,
              status: d.cureCheckupInfo.status,
              payStatus: d.payStatus,
              price: d.price ? Number.parseFloat(d.price) : 0,
              id: d.cureCheckupInfo.id,
              remark: d.cureCheckupInfo.remark,
              resultAddress: d.resultAddress
            };
            // arr.push(j);
            th.addItem(j);
          });
          th.$nextTick(function() {
            th.isOpenEdit = true;
            if (reset) {
              th.$store.commit("Set_templateSave", {
                key: "t3",
                value: false
              });
            }
          });
        }
      });
    },
    //选择实验室检查
    onOpenLab() {
      const th = this;
      th.dialogLab = true;
      th.$refs["inputLab"].blur();
    },
    //添加项目
    addItem(json) {
      const th = this;
      if (typeof json.payStatus == "undefined") {
        json.payStatus = 2;
      }
      if (typeof json.status == "undefined") {
        json.status = 0;
      }
      if (json.payStatus == 2 && json.status == 0) {
        // let data = th.form.find(d => {
        //   return (
        //     d.checkupId == json.checkupId &&
        //     json.payStatus == 2 &&
        //     json.status == 0
        //   );
        // });
        // if (data) {
        //   return;
        // }
        th.form.push(json);
      } else {
        th.form.push(json);
      }
    },
    //清空未付款的数据或未执行
    delListData() {
      const th = this;
      let newArr = th.form.filter(d => {
        return d.payStatus != 2 || d.status != 0;
      });
      th.form = newArr;
    },
    //判断是否可编辑
    toDisabled(obj) {
      if (obj.payStatus == 2 && obj.status == 0) {
        return false;
      } else {
        return true;
      }
    }
  },
  watch: {
    form: {
      deep: true,
      handler: function(n, o) {
        let th = this;
        if (!th.isOpenEdit) return;
        let bo = true;
        if (n.length == 0 && !th.cureMedicalRelateId) {
          bo = false;
        }
        //开启修改状态
        th.$store.commit("Set_templateSave", {
          key: "t3",
          value: bo
        });
      }
    },
    //监听大模板数据
    templateData: {
      deep: true,
      immediate: true,
      handler(n, o) {
        const th = this;
        let data = n.t3;
        //判断是否有值
        if (!data) {
          return;
        }
        //清空数据
        if (n.isEmpty) {
          th.delListData();
        }
        data.forEach(ele => {
          //添加数据
          let j = {
            cureId: th.cureId,
            hpId: th.adminUser.hpId,
            patientId: th.patientId,
            deptId: th.deptId,
            doctorId: th.doctorId,
            checkupId: ele.id,
            checkupNum: 1,
            checkName: ele.checkName,
            unit: ele.unit ? ele.unit : 0,
            sellMoney: "",
            price: ele.sellMoney,
            id: "",
            remark: ele.remark,
            resultAddress: ""
          };
          j.sellMoney = ele.sellMoney * j.checkupNum;
          th.addItem(j);
        });
      }
    }
  }
};
</script>
<style lang="scss" scoped>
.item-box {
  background-color: #f3f3f3;
  margin: 0;
  padding: 9px 10px;
  border-radius: 7px;
  cursor: pointer;
}
</style>
